package com.sky.mapper;

import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param order
     */
    void insert(Orders order);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 获取订单用户数据
     * @param ordersPageQueryDTO
     * @return
     */
    List<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     * @param toBeConfirmed
     * @return
     */
    @Select("select count(id) from orders where status = #{toBeConfirmed}")
    Integer countStatus(Integer toBeConfirmed);

    /**
     * 查询超时订单
     * @param status
     * @param now
     */
    @Select("select * from orders where status = #{status} and order_time < DATE_SUB(#{now}, INTERVAL #{time} MINUTE)")
    List<Orders> getTimeoutOrders(Integer status, LocalDateTime now, String time);

    /**
     * 根据状态统计订单营业额
     * @param status
     * @return
     */
    @Select("select sum(amount) from orders where status = #{status}")
    Double sumByMap(Map<String,? extends Serializable> status);

    Integer getOrderCount(@Param("begin") LocalDateTime beginTime, @Param("end") LocalDateTime endTime, @Param("status") Integer status);

    List<GoodsSalesDTO> getSalesTop10(@Param("begin") LocalDateTime beginTime, @Param("end") LocalDateTime endTime);
}
